package com.study.demo_jdbc_template.service;

import com.study.demo_jdbc_template.bean.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName: UserService
 * @Description:
 * @author: WS
 * @date 2020/12/14
 * @Version: 1.0
 **/
@Service
public class UserService {
    @Autowired
    JdbcTemplate template;
    public Integer addUser(User user){
        String sql="insert into users(name,age) value(?,?)";
        int update = template.update(sql, user.getName(), user.getAge());
        return update;
    }
    public Integer updateUser(User user){
        String sql="update users set name =? where id=?";
        int update = template.update(sql, user.getName(), user.getId());
        return update;
    }
    public Integer deleteUser(Integer id){
        String sql="delete from users where id=?";
        int delete = template.update(sql, id);
        return delete;
    }
    public List<User> getAllUsers(){
        String sql="select * from users";
        List<User> list = template.query(sql, new BeanPropertyRowMapper<>(User.class));
        return list;
    }
}
